package net.dopan.pigframe.demo.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.dopan.pigframe.core.base.controller.response.ResponseBean;
import net.dopan.pigframe.demo.mapper.UserConcernUserMapper;
import net.dopan.pigframe.demo.model.entity.vo.UserActivityCancleVo;
import net.dopan.pigframe.demo.model.entity.vo.UserFeedbackVo;
import net.dopan.pigframe.demo.service.UserConcernUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/personalCenter")
public class PersonalCenterController {

    @Autowired
    private UserConcernUserService userConcernUserService;
    @Autowired
    private HttpServletResponse response;

    @GetMapping("/getPersonalMsg")
    @ApiOperation("获取用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "当前用户Id", required = true, dataType = "String")})
    public ResponseBean getPersonalMsg(@RequestParam("userId") String userId) {

        /** 允许该域发起跨域请求 */
        response.setHeader("Access-Control-Allow-Origin", "*");
        /**  允许的外域请求方式 */
        response.setHeader("Access-Control-Allow-Methods", "POST, GET");
        return new ResponseBean(ResponseBean.SUCCESS, "", userConcernUserService.findUserVoByUserId(userId));
    }

    @GetMapping("/getUserNote")
    @ApiOperation("获取用户笔记")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "当前用户Id", required = true, dataType = "String")})
    public ResponseBean getUserNote(@RequestParam("userId") String userId, @RequestParam("pageSize") String pageSize, @RequestParam("pageNum") String pageNum) {

        /** 允许该域发起跨域请求 */
        response.setHeader("Access-Control-Allow-Origin", "*");
        /**  允许的外域请求方式 */
        response.setHeader("Access-Control-Allow-Methods", "POST, GET");
        int offset, limit;
        try {
            offset = (Integer.parseInt(pageNum) - 1) * Integer.parseInt(pageSize);
            limit = Integer.parseInt(pageSize);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return new ResponseBean(ResponseBean.FAIL, "pageNum和pageSize为数字", "");
        }
        return new ResponseBean(ResponseBean.SUCCESS, "", userConcernUserService.findUserNoteVoByUserId(userId,offset,limit));
    }

    @GetMapping("/getUserCollect")
    @ApiOperation("获取用户收藏")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "当前用户Id", required = true, dataType = "String")})
    public ResponseBean getUserCollect(@RequestParam("userId") String userId) {

        /** 允许该域发起跨域请求 */
        response.setHeader("Access-Control-Allow-Origin", "*");
        /**  允许的外域请求方式 */
        response.setHeader("Access-Control-Allow-Methods", "POST, GET");
        return new ResponseBean(ResponseBean.SUCCESS, "", userConcernUserService.findUserCollectMap(userId));
    }

    @GetMapping("/concernUser")
    @ApiOperation("用户关注(包括取消关注)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "当前用户Id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "concernUserId", value = "被关注者Id", required = true, dataType = "String")})
    public ResponseBean concernUser(@RequestParam("userId") String userId, @RequestParam("concernUserId") String concernUserId) {

        /** 允许该域发起跨域请求 */
        response.setHeader("Access-Control-Allow-Origin", "*");
        /**  允许的外域请求方式 */
        response.setHeader("Access-Control-Allow-Methods", "POST, GET");
        if (userId.equals(concernUserId)) {
            return new ResponseBean(ResponseBean.FAIL, "不可以关注自己!", "");
        }
        /** 关注 */
        if (userConcernUserService.isConcern(userId, concernUserId) == null) {
            userConcernUserService.concernUser(userId, concernUserId);
            return new ResponseBean(ResponseBean.SUCCESS, "关注成功！", userConcernUserService.fansCount(concernUserId));
        }
        /** 取消关注 */
        userConcernUserService.cancleConcernUser(userId, concernUserId);
        return new ResponseBean(ResponseBean.SUCCESS, "取消关注！", userConcernUserService.fansCount(concernUserId));
    }

    @GetMapping("/getConcernUsers")
    @ApiOperation("获取关注的人")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "当前用户Id", required = true, dataType = "String")})
    public ResponseBean getConcernUsers(@RequestParam("userId") String userId) {

        /** 允许该域发起跨域请求 */
        response.setHeader("Access-Control-Allow-Origin", "*");
        /**  允许的外域请求方式 */
        response.setHeader("Access-Control-Allow-Methods", "POST, GET");
        return new ResponseBean(ResponseBean.SUCCESS, "", userConcernUserService.getConcernUsers(userId));
    }

    @GetMapping("/getUserFans")
    @ApiOperation("获取粉丝")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "当前用户Id", required = true, dataType = "String")})
    public ResponseBean getUserFans(@RequestParam("userId") String userId) {

        /** 允许该域发起跨域请求 */
        response.setHeader("Access-Control-Allow-Origin", "*");
        /**  允许的外域请求方式 */
        response.setHeader("Access-Control-Allow-Methods", "POST, GET");
        return new ResponseBean(ResponseBean.SUCCESS, "", userConcernUserService.getUserFans(userId));
    }

    @GetMapping("/getUserActivityByUserId")
    @ApiOperation("获取用户参加的活动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "当前用户Id", required = true, dataType = "String")})
    public ResponseBean getUserActivityByUserId(@RequestParam("userId") String userId) {

        /** 允许该域发起跨域请求 */
        response.setHeader("Access-Control-Allow-Origin", "*");
        /**  允许的外域请求方式 */
        response.setHeader("Access-Control-Allow-Methods", "POST, GET");
        return new ResponseBean(ResponseBean.SUCCESS, "", userConcernUserService.getUserActivityByUserId(userId));
    }

    @DeleteMapping("/cancleUserActivity")
    @ApiOperation("取消活动(删除参加的过期活动)")
    @Transactional
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "当前用户Id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "activityId", value = "该活动Id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "flag", value = "标记取消为0删除为1", required = true, dataType = "String"),
            @ApiImplicitParam(name = "cancleReason", value = "活动取消原因(取消活动填写)", required = false, dataType = "String")})
    public ResponseBean cancleUserActivity(@RequestParam("userId") String userId, @RequestParam("activityId") String activityId,
                                           @RequestParam("flag") String flag, @RequestParam(value = "cancleReason", required = false) String cancleReason) {

        /** 允许该域发起跨域请求 */
        response.setHeader("Access-Control-Allow-Origin", "*");
        /**  允许的外域请求方式 */
        response.setHeader("Access-Control-Allow-Methods", "POST, GET");
        /** 取消活动 */
        if (flag.equals("0")) {
            UserActivityCancleVo userActivityCancleVo = new UserActivityCancleVo();
            userActivityCancleVo.setActivityId(activityId);
            userActivityCancleVo.setCancleTime(new Date());
            userActivityCancleVo.setUserId(userId);
            userActivityCancleVo.setContent(cancleReason);
            userConcernUserService.saveUserActivityCancle(userActivityCancleVo);
        }
        if (userConcernUserService.cancleUserActivity(userId, activityId) > 0) {
            return new ResponseBean(ResponseBean.SUCCESS, "取消成功!", "");
        }
        return new ResponseBean(ResponseBean.FAIL, "网络延时，稍后重试！", "");
    }

    @GetMapping("/getLaunchActivity")
    @ApiOperation("获取用户发起的活动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "当前用户Id", required = true, dataType = "String")})
    public ResponseBean getLaunchActivity(@RequestParam("userId") String userId) {

        /** 允许该域发起跨域请求 */
        response.setHeader("Access-Control-Allow-Origin", "*");
        /**  允许的外域请求方式 */
        response.setHeader("Access-Control-Allow-Methods", "POST, GET");
        return new ResponseBean(ResponseBean.SUCCESS, "", userConcernUserService.getLaunchActivityAndEnd(userId));
    }

    @DeleteMapping("delUserActivityByUser")
    @ApiOperation("删除自己发起的过期活动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "当前用户Id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "activityId", value = "该活动Id", required = true, dataType = "String")})
    public ResponseBean delUserActivityByUser(@RequestParam("userId") String userId, @RequestParam("activityId") String activityId) {

        /** 允许该域发起跨域请求 */
        response.setHeader("Access-Control-Allow-Origin", "*");
        /**  允许的外域请求方式 */
        response.setHeader("Access-Control-Allow-Methods", "POST, GET");
        if (userConcernUserService.delUserActivityByUserId(userId, activityId) > 0) {
            return new ResponseBean(ResponseBean.SUCCESS, "删除成功!", "");
        }
        return new ResponseBean(ResponseBean.FAIL, "网络延时，稍后重试！", "");
    }

    @GetMapping("editUserActivityByUser")
    @ApiOperation("编辑活动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "当前用户Id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "activityId", value = "该活动Id", required = true, dataType = "String")})
    public ResponseBean editUserActivityByUser(@RequestParam("userId") String userId, @RequestParam("activityId") String activityId) {

        /** 允许该域发起跨域请求 */
        response.setHeader("Access-Control-Allow-Origin", "*");
        /**  允许的外域请求方式 */
        response.setHeader("Access-Control-Allow-Methods", "POST, GET");
        return new ResponseBean(ResponseBean.SUCCESS, "", userConcernUserService.editUserActivityVo(userId, activityId));
    }

    @GetMapping("/getLikeUserActivity")
    @ApiOperation("获取用户感兴趣的活动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "当前用户Id", required = true, dataType = "String")})
    public ResponseBean getLikeUserActivity(@RequestParam("userId") String userId) {

        /** 允许该域发起跨域请求 */
        response.setHeader("Access-Control-Allow-Origin", "*");
        /**  允许的外域请求方式 */
        response.setHeader("Access-Control-Allow-Methods", "POST, GET");
        return new ResponseBean(ResponseBean.SUCCESS, "", userConcernUserService.getLikeUserActivity(userId));
    }

    @DeleteMapping("/delLikeUserActivity")
    @ApiOperation("删除用户感兴趣的活动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "当前用户Id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "activityId", value = "活动Id", required = true, dataType = "String")})

    public ResponseBean delLikeUserActivity(@RequestParam("userId") String userId, @RequestParam("activityId") String activityId) {

        /** 允许该域发起跨域请求 */
        response.setHeader("Access-Control-Allow-Origin", "*");
        /**  允许的外域请求方式 */
        response.setHeader("Access-Control-Allow-Methods", "POST, GET");
        if (userConcernUserService.delLikeUserActivity(userId, activityId) > 0) {
            return new ResponseBean(ResponseBean.SUCCESS, "删除成功", "");
        }
        return new ResponseBean(ResponseBean.FAIL, "网络延时，稍后重试!", "");
    }

    @GetMapping("/userFeedback")
    @ApiOperation("用户意见反馈")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "当前用户Id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "content", value = "反馈内容", required = true, dataType = "String")})

    public ResponseBean userFeedback(UserFeedbackVo userFeedbackVo) {

        /** 允许该域发起跨域请求 */
        response.setHeader("Access-Control-Allow-Origin", "*");
        /**  允许的外域请求方式 */
        response.setHeader("Access-Control-Allow-Methods", "POST, GET");
        return new ResponseBean(ResponseBean.SUCCESS, "意见提交成功", userConcernUserService.saveUserFeedBack(userFeedbackVo));
    }

    @GetMapping("/changeUserUUID")
    @ApiOperation("修改Id编号,uuid格式前段判断")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "当前用户Id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "uuid", value = "用户", required = true, dataType = "String")})

    public ResponseBean changeUserUUID(@RequestParam("userId") String userId, @RequestParam("uuid") String uuid) {

        /** 允许该域发起跨域请求 */
        response.setHeader("Access-Control-Allow-Origin", "*");
        /**  允许的外域请求方式 */
        response.setHeader("Access-Control-Allow-Methods", "POST, GET");
        int flag = userConcernUserService.findUserUUIDFlag(userId);
        if (flag == 1) {
            return new ResponseBean(ResponseBean.SUCCESS, "Id编号已经修改过", "");
        }
        return new ResponseBean(ResponseBean.SUCCESS, "修改成功!", userConcernUserService.changUserUUID(userId, uuid));
    }

    @GetMapping("/getCancleReason")
    @ApiOperation("获取取消原因列表")
    public ResponseBean getCancleReason() {

        /** 允许该域发起跨域请求 */
        response.setHeader("Access-Control-Allow-Origin", "*");
        /**  允许的外域请求方式 */
        response.setHeader("Access-Control-Allow-Methods", "POST, GET");
        return new ResponseBean(ResponseBean.SUCCESS, "", userConcernUserService.getCancleReason());
    }

    @GetMapping("/activityCalendar")
    @ApiOperation("活动日历")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "当前用户Id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "dateTime", value = "日期(yyyy.MM.dd)", required = true, dataType = "String")})

    public ResponseBean activityCalendar(@RequestParam("userId") String userId, @RequestParam("dateTime") String dateTime) {

        /** 允许该域发起跨域请求 */
        response.setHeader("Access-Control-Allow-Origin", "*");
        /**  允许的外域请求方式 */
        response.setHeader("Access-Control-Allow-Methods", "POST, GET");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
        Date date;
        try {
            date = sdf.parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
            return new ResponseBean(ResponseBean.FAIL, "日期格式为yyyy.MM.dd", "");
        }
        return new ResponseBean(ResponseBean.SUCCESS, "", userConcernUserService.activityCalendar(userId, date));
    }

    @GetMapping("/getUserEvent")
    @ApiOperation("获取个人动态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "当前用户Id", required = true, dataType = "String")})
    public ResponseBean getUserEvent(@RequestParam("userId") String userId, @RequestParam("pageSize") String pageSize, @RequestParam("pageNum") String pageNum) {

        /** 允许该域发起跨域请求 */
        response.setHeader("Access-Control-Allow-Origin", "*");
        /**  允许的外域请求方式 */
        response.setHeader("Access-Control-Allow-Methods", "POST, GET");
        int offset, limit;
        try {
            offset = (Integer.parseInt(pageNum) - 1) * Integer.parseInt(pageSize);
            limit = Integer.parseInt(pageSize);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return new ResponseBean(ResponseBean.FAIL, "pageNum和pageSize为数字", "");
        }

        return new ResponseBean(ResponseBean.SUCCESS, "", userConcernUserService.getUserEvent(userId,offset,limit));
    }

}